package org.dromara.genealogy.controller;

import java.util.List;

import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.bo.SysDeptBo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.genealogy.domain.vo.GeDirectoryVo;
import org.dromara.genealogy.domain.bo.GeDirectoryBo;
import org.dromara.genealogy.service.IGeDirectoryService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 目录管理
 *
 * @author qianjm
 * @date 2025-08-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/genealogy/directory")
public class GeDirectoryController extends BaseController {

    private final IGeDirectoryService geDirectoryService;

    /**
     * 查询目录管理列表
     */
    @SaCheckPermission("genealogy:directory:list")
    @GetMapping("/list")
    public TableDataInfo<GeDirectoryVo> list(GeDirectoryBo bo, PageQuery pageQuery) {
        return geDirectoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取目录管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("genealogy:directory:query")
    @GetMapping("/{id}")
    public R<GeDirectoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(geDirectoryService.queryById(id));
    }

    /**
     * 新增目录管理
     */
    @SaCheckPermission("genealogy:directory:add")
    @Log(title = "目录管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GeDirectoryBo bo) {
        return toAjax(geDirectoryService.insertByBo(bo));
    }

    /**
     * 修改目录管理
     */
    @SaCheckPermission("genealogy:directory:edit")
    @Log(title = "目录管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GeDirectoryBo bo) {
        return toAjax(geDirectoryService.updateByBo(bo));
    }

    /**
     * 删除目录管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("genealogy:directory:remove")
    @Log(title = "目录管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(geDirectoryService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 校验目录编码是否唯一
     *
     * @param  bo
     */
    @SaCheckPermission("genealogy:directory:query")
    @PostMapping("/checkUnique")
    public R<Void> checkUnique(@RequestBody GeDirectoryBo bo) {
        return toAjax(geDirectoryService.checkUnique(bo));
    }

    /**
     * 获取目录树列表
     */
    @SaCheckPermission("genealogy:directory:query")
    @GetMapping("/directoryTree")
    public R<List<Tree<Long>>> directoryTree(GeDirectoryBo bo) {
        return R.ok(geDirectoryService.selectDirecTreeList(bo));
    }
}
